#!/bin/bash

set -e

# Color
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
TIMESTAMP=$(date +"%Y-%m-%d_%H:%M:%S")
LOG_FILE=${TIMESTAMP}.log


function log_print()
{
	local message="$@"
	local file_name=$(basename "${BASH_SOURCE[1]}")
	local line_number=${BASH_LINENO[0]}
	local function_name=${FUNCNAME[1]}
	local ts=$(date +"%Y-%m-%d_%H:%M:%S")
	local output="${GREEN}[${ts}][${file_name}:${line_number}][${function_name}] ${message}${NC}"

	echo -e "${output}" | tee -a "${LOG_FILE}"
}

function log_print_debug()
{
	local message="$@"
	local file_name=$(basename "${BASH_SOURCE[1]}")
	local line_number=${BASH_LINENO[0]}
	local function_name=${FUNCNAME[1]}
	local ts=$(date +"%Y-%m-%d_%H:%M:%S")
	local output="[${ts}][${file_name}:${line_number}][${function_name}] ${message}"

	echo -e "${output}" | tee -a "${LOG_FILE}"
}

function log_print_error()
{
	local message="$@"
	local file_name=$(basename "${BASH_SOURCE[1]}")
	local line_number=${BASH_LINENO[0]}
	local function_name=${FUNCNAME[1]}
	local ts=$(date +"%Y-%m-%d_%H:%M:%S")
	local output="${RED}[${ts}][${file_name}:${line_number}][${function_name}] ${message}${NC}"

	echo -e "${output}" | tee -a "${LOG_FILE}"
}

function command_install()
{
	if [[ $# != 0 ]]; then
		for arg in $*; do
			if command -v ${arg} >/dev/null 2>&1; then
				log_print "${arg} command has been installed"
			else
				pacman -S --noconfirm "${arg}" 2>&1 | tee -a "${LOG_FILE}"
			fi
		done
	fi
}

function arch_logo()
{
	echo "" | tee -a "${LOG_FILE}"
	echo "    █     ▀██▀▀█▄     ▄▄█▀▀▀▄█ ▀██▀  ▀██▀    ▀██▀      ▀██▀ ▀█▄   ▀█▀ ▀██▀  ▀█▀ ▀██▀ ▀█▀" | tee -a "${LOG_FILE}"
	echo "   ███     ██   ██  ▄█▀     ▀   ██    ██      ██        ██   █▀█   █   ██    █    ██ █  " | tee -a "${LOG_FILE}"
	echo "  █  ██    ██▀▀█▀   ██          ██▀▀▀▀██      ██        ██   █ ▀█▄ █   ██    █     ██   " | tee -a "${LOG_FILE}"
	echo " ▄▀▀▀▀█▄   ██   █▄  ▀█▄      ▄  ██    ██      ██        ██   █   ███   ██    █    █ ██  " | tee -a "${LOG_FILE}"
	echo "▄█▄  ▄██▄ ▄██▄  ▀█▀  ▀▀█▄▄▄▄▀  ▄██▄  ▄██▄    ▄██▄▄▄▄▄█ ▄██▄ ▄█▄   ▀█    ▀█▄▄▀   ▄█   ██▄" | tee -a "${LOG_FILE}"
	echo "" | tee -a "${LOG_FILE}"
}

# Step1
function arch_self_check()
{
	log_print "[Step1] start to self check"

	log_print "Check network connection"
	ping -c 4 baidu.com
	if [ $? -eq 0 ]; then
		log_print "ping baidu.com success"
	else
		log_print_error "ping baidu.com failed"
		exit 1
	fi

	log_print "[Step1] self check finished"
}

# Step2
function arch_update_sources()
{
	log_print "[Step2] start to update arch sources"

	if grep "archlinux" /etc/pacman.conf >/dev/null 2>&1; then
		log_print "Tsinghua Source has been installed"
	else
		echo -e 'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch' > /etc/pacman.d/mirrorlist | tee -a "${LOG_FILE}"
	fi

	pacman -Syy --noconfirm 2>&1 | tee -a "${LOG_FILE}"
	pacman-key --init 2>&1 | tee -a "${LOG_FILE}"
	pacman-key --populate archlinux 2>&1 | tee -a "${LOG_FILE}"
	pacman -Sy archlinux-keyring --noconfirm 2>&1 | tee -a "${LOG_FILE}"

	if grep "archlinuxcn" /etc/pacman.conf >/dev/null 2>&1; then
		log_print "Tsinghua Source archlinuxcn has been installed"
	else
		echo -e '[archlinuxcn]' >> /etc/pacman.conf | tee -a "${LOG_FILE}"
		echo -e 'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch' > /etc/pacman.conf | tee -a "${LOG_FILE}"
	fi

	log_print "[Step2] update arch sources finished"
}

# Step3
function arch_install_base()
{
	log_print "[Step3] start to install base"

	command_install which tree zip unzip htop wget curl git tig openssh make cmake base-devel net-tools tzdata fzf \
		tmux ranger neovim neofetch ctags mosh ripgrep shellcheck the_silver_searcher lzma zathura python python-pip \
		alacritty fontconfig noto-fonts-emoji

	sed -i "s/#permitrootlogin.*/permitrootlogin yes/g" /etc/ssh/sshd_config 2>&1 | tee -a "${LOG_FILE}"
	log_print "config sshd permit finished"

	ssh-keygen -A 2>&1 | tee -a "${LOG_FILE}"
	log_print "generate ssh key finished"

	git config --global user.name 'zhengqijun' 2>&1 | tee -a "${LOG_FILE}"
	git config --global user.email 'zhengqijun0121@qq.com' 2>&1 | tee -a "${LOG_FILE}"
	log_print "config git finished"

	log_print "[Step3] install base finished"
}

# Step4
function arch_install_dotfiles()
{
	log_print "[Step4] start to install dotfiles"

	git clone https://gitee.com/zhengqijun/oh-my-dotfiles ~/.oh-my-dotfiles --depth=1 2>&1 | tee -a "${LOG_FILE}"
	log_print "download dotfiles finished"

	ln -s ~/.oh-my-dotfiles/bash/bashrc.bash ~/.bashrc 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/bash/bash_profile.bash ~/.bash_profile 2>&1 | tee -a "${LOG_FILE}"
	log_print "config bash finished"

	ln -s ~/.oh-my-dotfiles/npm/npmrc ~/.npmrc 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/nvm ~/.nvm --depth=1 2>&1 | tee -a "${LOG_FILE}"
	export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node 2>&1 | tee -a "${LOG_FILE}"
	export NVM_IOJS_ORG_MIRROR=http://npm.taobao.org/mirrors/iojs 2>&1 | tee -a "${LOG_FILE}"
	source ~/.nvm/nvm.sh 2>&1 | tee -a "${LOG_FILE}"
	nvm install v16.18.0 2>&1 | tee -a "${LOG_FILE}"
	nvm alias default v16.18.0 2>&1 | tee -a "${LOG_FILE}"
	log_print "config nvm finished"

	ln -s ~/.oh-my-dotfiles/tmux/tmux.conf ~/.tmux.conf 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/tmux/tmux.conf.local ~/.tmux.conf.local 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tpm ~/.tmux/plugins/tpm --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tmux-sensible ~/.tmux/plugins/tmux-sensible --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tmux-sidebar ~/.tmux/plugins/tmux-sidebar --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/vim-tmux-navigator ~/.tmux/plugins/vim-tmux-navigator --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tmux-jump ~/.tmux/plugins/tmux-jump --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tmux-yank ~/.tmux/plugins/tmux-yank --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tmux-copycat ~/.tmux/plugins/tmux-copycat --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tmux-resurrect ~/.tmux/plugins/tmux-resurrect --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tmux-continuum ~/.tmux/plugins/tmux-continuum --depth=1 2>&1 | tee -a "${LOG_FILE}"
	git clone https://gitee.com/zhengqijun/tmux-logging ~/.tmux/plugins/tmux-logging --depth=1 2>&1 | tee -a "${LOG_FILE}"
	log_print "config tmux finished"

	mkdir -p ~/.local/share 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/fonts ~/.local/share/fonts 2>&1 | tee -a "${LOG_FILE}"
	cd ~/.local/share/fonts && mkfontscale && mkfontdir && fc-cache -fv 2>&1 | tee -a "${LOG_FILE}"
	log_print "config fonts finished"

	mkdir -p ~/.config 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/ranger ~/.config/ranger 2>&1 | tee -a "${LOG_FILE}"
	log_print "config ranger finished"

	ln -s ~/.oh-my-dotfiles/neofetch ~/.config/neofetch 2>&1 | tee -a "${LOG_FILE}"
	log_print "config neofetch finished"

	ln -s ~/.oh-my-dotfiles/pip ~/.pip 2>&1 | tee -a "${LOG_FILE}"
	log_print "config pip finished"

	mkdir -p ~/.config/nvim 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/nvim/init.lua ~/.config/nvim/init.lua 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/nvim/lua ~/.config/nvim/lua 2>&1 | tee -a "${LOG_FILE}"
	pip3 install pynvim --no-cache-dir 2>&1 | tee -a "${LOG_FILE}"
	nvim --headless -c 'autocmd User PackerComplete quitall' -c 'PackerSync' 2>&1 | tee -a "${LOG_FILE}"
	log_print "config neovim finished"

	mkdir -p ~/.vim 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/vim/vimrc ~/.vimrc 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/vim/colors ~/.vim/colors 2>&1 | tee -a "${LOG_FILE}"
	ln -s ~/.oh-my-dotfiles/vim/files ~/.vim/files 2>&1 | tee -a "${LOG_FILE}"
	vim -c PlugInstall +qall 2>&1 | tee -a "${LOG_FILE}"

	log_print "[Step4] install dotfiles finished"
}

# Step5
function arch_install_dwm()
{
	log_print "[Step5] start to install dwm"

	git clone https://gitee.com/zhengqijun/dwm ~/.dwm --depth=1 2>&1 | tee -a "${LOG_FILE}"
	cd ~/.dwm && make clean install 2>&1 | tee -a "${LOG_FILE}"

	git clone https://gitee.com/zhengqijun/slstatus ~/.slstatus --depth=1 2>&1 | tee -a "${LOG_FILE}"
	cd ~/.slstatus && make clean install --depth=1 2>&1 | tee -a "${LOG_FILE}"

	log_print "[Step5] install dwm finished"
}

function main()
{
	arch_logo
	arch_self_check
	arch_update_sources
	arch_install_base
	arch_install_dotfiles
	arch_install_dwm
	arch_logo

	log_print "The configuration of archlinux has been completed. Enjoy it!"
}

main

# EOF

